package net.qhzw.vip.qiyu.util;

import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.handler.MyException;
import org.joda.time.Days;
import org.joda.time.LocalDate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class MyDateUtils {


    /***
     * 获取当前年月日日期
     * @return
     */
    public static String getCurDt() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        return sdf.format(new Date());
    }

    public static String getCurMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        return sdf.format(new Date());
    }

    /***
     * 获取当前年月日日期
     * @return
     */
    public static String getCurDt2() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }


    /***
     * 根据yyyy年MM月dd日 转换日期
     * @return
     */
    public static Date getDateByString(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date parse = null;
        try {
            parse = sdf.parse(strDate);
        } catch (ParseException e) {
            log.error("日期转换异常");
        }
        return parse;
    }


    /***
     * 获取两个时间相差的天数
     * @param one
     * @param two
     * @return
     */
    public static long daysBetween(Date one, Date two) {
        return Math.abs(Days.daysBetween(new LocalDate(one.getTime()),
                new LocalDate(two.getTime())).getDays());
    }




    public static Date getNextMonth19day() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        String format = df.format(calendar.getTime());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < format.split("-").length - 1; i++) {
            if (i != format.split("-").length) {
                sb.append(format.split("-")[i]).append("-");
            }
        }
        sb.append(19);
        try {
            Date parse = df.parse(sb.toString());
            return parse;
        } catch (ParseException e) {
            throw new MyException("日期转换错误");
        }
    }

    public static String getDateString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        return sdf.format(date);
    }


    public static String getTodayStart() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return sdf.format(new Date());
    }

    public static String getTodayEnd() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        return sdf.format(new Date());
    }

    public static void main(String[] args) {
        long l = MyDateUtils.daysBetween(new Date(), new Date());
        System.out.println(MyDateUtils.getCurMonth());

        System.out.println(l);
    }

}
